package com.it.controller;

import com.github.pagehelper.PageInfo;
import com.it.pojo.Discretion;
import com.it.pojo.Product;
import com.it.pojo.UserInfo;
import com.it.pojo.dto.DiscretionDto;
import com.it.pojo.dto.ProductDto;
import com.it.pojo.dto.TimeDto;
import com.it.service.DiscretionService;
import com.it.service.UserInfoService;
import com.it.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/29 10:17
 */
@Api(value = "DiscretionController", tags = {"自行处理"})
@Controller
@RequestMapping("/discretion")
public class DiscretionController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private DiscretionService discretionService;

    @ApiOperation("查看产废记录")
    @GetMapping("/list")
    public ModelAndView find(DiscretionDto dto){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/discretion/list");
        PageInfo<Discretion> pageInfo = discretionService.findPage(dto);
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserInfo> users = userInfoService.findAll();
        modelAndView.addObject("users",users);
        List<TimeDto> dtoList = new ArrayList<>();
        dtoList.add(new TimeDto(1,"当天"));
        dtoList.add(new TimeDto(2,"本月"));
        dtoList.add(new TimeDto(3,"本季"));
        dtoList.add(new TimeDto(4,"本年"));
        modelAndView.addObject("timeList",dtoList);
        return modelAndView;
    }

    @PostMapping("/list")
    public ModelAndView one(DiscretionDto dto){
        System.out.println(dto);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/discretion/list");
        PageInfo<Discretion> pageInfo = discretionService.findPage(dto);
        modelAndView.addObject("pageInfo",pageInfo);
        List<UserInfo> users = userInfoService.findAll();
        modelAndView.addObject("users",users);
        List<TimeDto> dtoList = new ArrayList<>();
        dtoList.add(new TimeDto(1,"当天"));
        dtoList.add(new TimeDto(2,"本月"));
        dtoList.add(new TimeDto(3,"本季"));
        dtoList.add(new TimeDto(4,"本年"));
        modelAndView.addObject("timeList",dtoList);
        return modelAndView;
    }

    @PostMapping("/excel")
    public ModelAndView excel(DiscretionDto dto, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/discretion/list");
        /*导入excel操作*/
        List<Discretion> excel = discretionService.findExcel(dto);
        ExcelUtils.export(response,"自行处理记录",excel,Discretion.class);
        return modelAndView;
    }

}
